import requests


def download_audio(url, filename):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
        "Referer": "https://www.ximalaya.com/sound/772125003"
    }

    try:
        response = requests.get(url, headers=headers, stream=True, timeout=10)
        response.raise_for_status()

        with open(filename, 'wb') as f:
            for chunk in response.iter_content(chunk_size=8192):
                f.write(chunk)
        print(f"音频文件已成功下载到：{filename}")

    except requests.exceptions.RequestException as e:
        print(f"下载失败，错误信息：{str(e)}")
        return False
    return True


if __name__ == "__main__":
    audio_url = "https://audiopay.cos.tx.xmcdn.com/download/1.0.0/storages/4479-audiopay/F6/30/GKwRINsLAnCKABaS9gMrcqDv.m4a?sign=753eb78b4754cbc959a6cf5d3d9ad221&buy_key=FM&timestamp=1748359381761000&token=7679&duration=242"
    file_name = "ximalaya_audio.m4a"

    download_audio(audio_url, file_name)